package org.light.simpleauth.verb;

import java.util.ArrayList;
import java.util.List;

import org.light.complexverb.ListMyActive;
import org.light.core.Verb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.easyui.EasyUIPositions;
import org.light.exception.ValidateException;
import org.light.utils.WriteableUtil;
import org.light.verb.FindByName;

public class ReadMySession extends Verb implements EasyUIPositions {
	protected Domain roleDomain;
	protected Domain privilegeDomain;	

	@Override
	public Method generateDaoImplMethod() throws Exception {
		return null;
	}	

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		return null;		
	}

	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		return null;
	}
	@Override
	public Method generateServiceImplMethod() throws Exception  {
		return null;
	}
	
	public ReadMySession(){
		super();
		this.setLabel("读出会话");
	}
	
	public ReadMySession(Domain domain) throws ValidateException{
		super();
		this.domain = domain;
		this.denied = domain.isVerbDenied("ReadMySession");
		this.setVerbName("ReadMySession");
		this.setLabel("读出会话");
		if  (domain.getLanguage().equalsIgnoreCase("english"))  this.setLabel("ReadMySession");
	}

	@Override
	public Method generateControllerMethod() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("ReadMySession");
			method.addSignature(new Signature(1,"c","*gin.Context"));
			
			List<Writeable> sList = new ArrayList<Writeable>();
			
			sList.add(new Statement(1000L,1,"username, err := session.ValidateJWTToken(c)"));
			sList.add(new Statement(2000L,1,"if err == nil && username != \"\" {"));
			
			FindByName find = new FindByName(this.domain);
			ListMyActive listma = new ListMyActive(this.domain,this.roleDomain);
			ListMyActive listpma = new ListMyActive(this.roleDomain,this.privilegeDomain);
			
			sList.add(new Statement(3000L,2,""+this.domain.getLowerFirstDomainName()+" := "+this.domain.getServiceimplSuffix()+"."+find.getVerbName()+"(username)"));
			sList.add(new Statement(4000L,2,"roles := "+this.domain.getServiceimplSuffix()+"."+listma.getVerbName()+"("+this.domain.getLowerFirstDomainName()+".Get"+this.domain.getDomainId().getCapFirstFieldName()+"())"));
			sList.add(new Statement(5000L,2,"perms := [] "+this.domain.getDomainSuffix()+"."+this.privilegeDomain.getCapFirstDomainNameWithSuffix()+"{}"));
			sList.add(new Statement(6000L,2,"for _, role := range roles {"));
			sList.add(new Statement(7000L,3,"cperms := "+this.domain.getServiceimplSuffix()+"."+listpma.getVerbName()+"(role.Get"+this.roleDomain.getDomainId().getCapFirstFieldName()+"())"));
			sList.add(new Statement(8000L,3,"for _, cperm := range cperms {"));
			sList.add(new Statement(9000L,4,"perms = append(perms, cperm)"));
			sList.add(new Statement(10000L,3,"}"));
			sList.add(new Statement(11000L,2,"}"));
			sList.add(new Statement(12000L,2,"roleNames := [] string{}"));
			sList.add(new Statement(13000L,2,"for _, role := range roles {"));
			sList.add(new Statement(14000L,3,"roleNames = append(roleNames, role.Get"+this.roleDomain.getDomainName().getCapFirstFieldName()+"())"));
			sList.add(new Statement(15000L,2,"}"));
			sList.add(new Statement(16000L,2,""));
			sList.add(new Statement(17000L,2,"permNames := [] string{}"));
			sList.add(new Statement(18000L,2,"for _, perm := range perms {"));
			sList.add(new Statement(19000L,3,"permNames = append(permNames, perm.Get"+this.privilegeDomain.getDomainName().getCapFirstFieldName()+"())"));
			sList.add(new Statement(20000L,2,"}"));
			sList.add(new Statement(21000L,2,""));
			sList.add(new Statement(22000L,2,"var permNamesMap = make(map[string]string)"));
			sList.add(new Statement(23000L,2,"for _,v:= range permNames{"));
			sList.add(new Statement(24000L,3,"permNamesMap[v] = v"));
			sList.add(new Statement(25000L,2,"}"));
			sList.add(new Statement(26000L,2,"var singlePermNames []string"));
			sList.add(new Statement(27000L,2,"for _,value := range permNamesMap{"));
			sList.add(new Statement(28000L,3,"singlePermNames = append(singlePermNames ,value)"));
			sList.add(new Statement(29000L,2,"}"));
			sList.add(new Statement(30000L,3,""));
			sList.add(new Statement(31000L,2,"c.JSON(http.StatusOK, gin.H{"));
			sList.add(new Statement(32000L,3,"\"user\":    "+this.domain.getLowerFirstDomainName()+","));
			sList.add(new Statement(33000L,3,"\"roles\":   roleNames,"));
			sList.add(new Statement(34000L,3,"\"perms\":   singlePermNames,"));
			sList.add(new Statement(35000L,3,"\"success\": true,"));
			sList.add(new Statement(36000L,2,"})"));
			sList.add(new Statement(37000L,2,"return"));
			sList.add(new Statement(38000L,1,"}"));
			sList.add(new Statement(39000L,1,"SendResponse(c, nil, nil, 0, false)"));

			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}	
	@Override
	public JavascriptBlock generateEasyUIJSButtonBlock() throws Exception {
		return null;	
	}

	@Override
	public JavascriptMethod generateEasyUIJSActionMethod() throws Exception {
		return null;		
	}

	public Domain getRoleDomain() {
		return roleDomain;
	}

	public void setRoleDomain(Domain roleDomain) {
		this.roleDomain = roleDomain;
	}

	public Domain getPrivilegeDomain() {
		return privilegeDomain;
	}

	public void setPrivilegeDomain(Domain privilegeDomain) {
		this.privilegeDomain = privilegeDomain;
	}

	@Override
	public Method generateDummyDaoImplMethod() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Method generateDummyServiceImplMethod() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
}
